package com.order.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.order.service.InitialService;
import com.order.service.OrderService;
import com.order.view.ItemView;
import com.order.view.OrderView;
import com.vrsys.domain.Order;

@Controller
public class OrderController {
    @Autowired
    private InitialService catchController;
    @Autowired
    private OrderService orderService;

    @RequestMapping("/index.od")
    public String initIndexPage(ModelMap model){
        model.addAttribute("groups", catchController.getGroupList());
        return "index";
    }

    @RequestMapping(value = "/order.od", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody List<Order> generOrder(@RequestBody List<ItemView> itemView){
        if (itemView != null && itemView.size() > 0) 
            orderService.save(itemView);
        return orderService.getOrderView().getOrders();
    }
}
